// app.js
const { init } = require('./wxCloudClientSDK.umd.js');

App({
  globalData: {
    userInfo: null,
    logged: false,
    usersID: null,
    latitude: 0,
    longitude: 0
  },

  onLaunch: function() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || [];
    logs.unshift(Date.now());
    wx.setStorageSync('logs', logs);

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // 这里可以添加发送 res.code 到后台的代码
      }
    });

    // 初始化云开发环境
    wx.cloud.init({
      env: "album-2g8qfq4rdbf96e9b", // 当前的云开发环境 ID
    });

    // 检查用户登录状态
    this.checkLoginStatus();
  },

  checkLoginStatus: function() {
    const that = this;
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      // 用户未登录，跳转到登录页面
      wx.navigateTo({
        url: '/pages/login/login'
      });
    } else {
      // 用户已登录，更新全局变量
      that.globalData.logged = true;
      that.globalData.usersID = userInfo.usersID;
      that.globalData.userInfo = userInfo;
    }
  }
});

const client = init(wx.cloud);
const models = client.models;